<?php
	if (empty($_GET['term'])) exit ;
	$q = strtoupper($_GET["term"]);

	include 'config.php';
	
	$connection = new mysqli($host, $user, $password, $db);
	if ($connection->connect_errno) {
		echo "Failed to connect to MySQL: (" . $connection->connect_errno . ") " . $connection->connect_error;
	}
	
	$codigos = array();
	$query = "SELECT id, codigo FROM cie WHERE codigo LIKE '" . $q . "%' AND !ISNULL(id_rango_cie_fk)"; 
	
	$res = $connection->query($query);
	for ($row_no = 0; $row_no < $res->num_rows - 1; $row_no++) {
		$res->data_seek($row_no);
		$row = $res->fetch_assoc();
		array_push($codigos, array("label" => htmlentities($row['codigo']), "value" => $row['id']));
		//array_push($result, array("id"=>$value, "label"=>$key, "value" => strip_tags($key)));
	}
	
	echo json_encode($codigos);
?>